import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:pcm_player/pcm_player.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  PcmPlayer player = new PcmPlayer();

  @override
  void initState() {
    super.initState();
    testPlayPcm();
  }

  void testPlayPcm() async {
    final List files = ['王后', '白方'];

    await player.initialize();
    await player.play();
    for(int i = 0; i < files.length; i++) {
      try {
        ByteData byteDate = await rootBundle.load("assets/${files[i]}.pcm");
        if (byteDate != null) {
          Uint8List stream = Uint8List.sublistView(byteDate);
          await player.feed(stream);
        } else {
          print('file content is null!');
        }
      } catch (e) {
        print(e.toString());
      }
    }
    //player.stop();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('hello pcm player plugin!\n'),
        ),
      ),
    );
  }
}
